

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
    <meta name="description" content="@[toc]Gitee 仓库地址：https:&#x2F;&#x2F;gitee.com&#x2F;ccuni&#x2F;pymongo-douban-comment-analysis 一、运行环境  Windows10 python 3.9 Anaconda3 +  jupyter mongodb 5.0.6 PyMongo 3.5.1  wordcloud-1.8.1  anaconda 安装 wordcloud的命令 1conda">
<meta property="og:type" content="article">
<meta property="og:title" content="MongoDB 项目实战 基于PyMongo的电影影评分析 _ 对数据结果进行可视化展示以及分析 _ 评论词云 _ 分时间段分析">
<meta property="og:url" content="http://example.com/2022/05/06/MongoDB%20%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98%20%E5%9F%BA%E4%BA%8EPyMongo%E7%9A%84%E7%94%B5%E5%BD%B1%E5%BD%B1%E8%AF%84%E5%88%86%E6%9E%90%20_%20%E5%AF%B9%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%9C%E8%BF%9B%E8%A1%8C%E5%8F%AF%E8%A7%86%E5%8C%96%E5%B1%95%E7%A4%BA%E4%BB%A5%E5%8F%8A%E5%88%86%E6%9E%90%20_%20%E8%AF%84%E8%AE%BA%E8%AF%8D%E4%BA%91%20_%20%E5%88%86%E6%97%B6%E9%97%B4%E6%AE%B5%E5%88%86%E6%9E%90/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="@[toc]Gitee 仓库地址：https:&#x2F;&#x2F;gitee.com&#x2F;ccuni&#x2F;pymongo-douban-comment-analysis 一、运行环境  Windows10 python 3.9 Anaconda3 +  jupyter mongodb 5.0.6 PyMongo 3.5.1  wordcloud-1.8.1  anaconda 安装 wordcloud的命令 1conda">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/458eb733a31e406bba10bbf88b299b69.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6b1ae64eb14f456a8b987605e795b405.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/19e9e7a11cef4c4c96a83440b25e6492.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/6d8a6ee9353f4f4ebae990dbf3069aa0.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/e01b0213c5a9469ebd82dec1930037d9.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/870336e2fc9f48a5b5aebf76d2b42012.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/31b22972521840a5854fa0e264728dfe.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/f5257a0bde1b415c9ca322d0bdebe679.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/51d95a6d4ad44ec9bb10341e9b898c62.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/a3716363e01b488f9b98d8710d20e6b6.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4072e8685f2342a0b65365d810d1f55d.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/317a05e38ff6438d9945ae95e49078de.png#=60%x">
<meta property="og:image" content="https://img-blog.csdnimg.cn/4ba600f459bd485e9c91a17ad489b768.png#=60%x">
<meta property="og:image" content="https://img-blog.csdnimg.cn/bcaab3363b314b82b2bda81c61fe5f91.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/c65527842e9b4bfebbeaed230db4a84f.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/bc74787eec8b4100b31ee1e7392a0edd.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/69ad5edec43e418da9fa0436b76f1be9.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/ba3ab45e06f54847b836fa6a63802dea.png#=60%x">
<meta property="og:image" content="https://img-blog.csdnimg.cn/faf7f285b1c946e8b3f511a062e2b1c6.png#=60%x">
<meta property="article:published_time" content="2022-05-06T09:20:35.000Z">
<meta property="article:modified_time" content="2022-08-22T15:46:09.932Z">
<meta property="article:author" content="John Doe">
<meta property="article:tag" content="MongoDB">
<meta property="article:tag" content="项目实战">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/458eb733a31e406bba10bbf88b299b69.png">
  
  
  
  <title>MongoDB 项目实战 基于PyMongo的电影影评分析 _ 对数据结果进行可视化展示以及分析 _ 评论词云 _ 分时间段分析 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>尤 Ni&#39;s Blog</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="MongoDB 项目实战 基于PyMongo的电影影评分析 _ 对数据结果进行可视化展示以及分析 _ 评论词云 _ 分时间段分析"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        John Doe
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-05-06 17:20" pubdate>
          2022年5月6日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          8.1k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          68 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">MongoDB 项目实战 基于PyMongo的电影影评分析 _ 对数据结果进行可视化展示以及分析 _ 评论词云 _ 分时间段分析</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：1 小时前
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <p>@[toc]<br>Gitee 仓库地址：<a target="_blank" rel="noopener" href="https://gitee.com/ccuni/pymongo-douban-comment-analysis">https://gitee.com/ccuni/pymongo-douban-comment-analysis</a></p>
<h1 id="一、运行环境"><a href="#一、运行环境" class="headerlink" title="一、运行环境"></a>一、运行环境</h1><hr>
<ul>
<li>Windows10</li>
<li>python 3.9</li>
<li>Anaconda3 +  jupyter</li>
<li>mongodb 5.0.6</li>
<li>PyMongo 3.5.1 </li>
<li>wordcloud-1.8.1</li>
</ul>
<p>anaconda 安装 wordcloud的命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs shell">conda install -c conda-forge wordcloud<br></code></pre></td></tr></table></figure>
<h1 id="二、实战介绍"><a href="#二、实战介绍" class="headerlink" title="二、实战介绍"></a>二、实战介绍</h1><hr>
<p>数据来源基于Python的第三方库，即<code>requests库</code>，<code>bs4库</code>，<code>re库</code>爬取豆瓣网TOP10的电影信息，以及它们的部分影评信息（100个左右）。</p>
<p>将爬取的信息进行预处理，封装成dict字典，借助 <code>pymongo库</code> 连接本机的 MongoDB，向数据库插入之前爬取的真实数据，然后分别使用MongoDB提供的map_reduce机制以及agreegate机制来聚合、分组、汇总计算数据，以MongoDB为基础，存储影视信息和评论信息，同时分析电影的综合价值。</p>
<h1 id="三、获取数据"><a href="#三、获取数据" class="headerlink" title="三、获取数据"></a>三、获取数据</h1><hr>
<p>这一部分可参考Gitee仓库：<a target="_blank" rel="noopener" href="https://gitee.com/ccuni/pymongo-douban-comment-analysis">https://gitee.com/ccuni/pymongo-douban-comment-analysis</a></p>
<h1 id="四、PyMongo-实战"><a href="#四、PyMongo-实战" class="headerlink" title="四、PyMongo 实战"></a>四、PyMongo 实战</h1><hr>
<h2 id="4-1-连接MongoDB、创建集合"><a href="#4-1-连接MongoDB、创建集合" class="headerlink" title="4.1 连接MongoDB、创建集合"></a>4.1 连接MongoDB、创建集合</h2><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs py"><span class="hljs-keyword">from</span> pymongo <span class="hljs-keyword">import</span> MongoClient<br><span class="hljs-keyword">from</span> random <span class="hljs-keyword">import</span> randint<br>client = MongoClient(<span class="hljs-string">&#x27;localhost&#x27;</span>, <span class="hljs-number">27017</span>)<br><br>db = client.mv<br><span class="hljs-comment"># 创建电影信息集合</span><br>ct_mv_info = db.dc_mv_info<br><span class="hljs-comment"># 创建影评集合</span><br>ct_mv_review = db.dc_mv_review<br><br><span class="hljs-comment"># 查看创建结果</span><br>ct_mv_review<br><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/458eb733a31e406bba10bbf88b299b69.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-2-向-MongoDB-插入文档"><a href="#4-2-向-MongoDB-插入文档" class="headerlink" title="4.2 向 MongoDB 插入文档"></a>4.2 向 MongoDB 插入文档</h2><p>这里先将DataFrame的影视信息转化为dict字典格式</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs py">dc_mv = []<br>index = <span class="hljs-number">0</span> <br><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> mv_data.values.tolist():<br>    dict_info = &#123;&#125;<br>    <span class="hljs-comment"># 指定文档的_id为电影ID</span><br>    dict_info[<span class="hljs-string">&#x27;_id&#x27;</span>] = mv_data[<span class="hljs-string">&#x27;mv_id&#x27;</span>][index]<br>    index += <span class="hljs-number">1</span><br>    <span class="hljs-comment"># i 用于循环遍历取DF列表数据</span><br>    i = <span class="hljs-number">0</span><br>    <span class="hljs-keyword">for</span> key, v <span class="hljs-keyword">in</span> mv_data.items():<br>        dict_info[key] = x[i]<br>        i += <span class="hljs-number">1</span><br>    <span class="hljs-comment"># 指定文档的</span><br>    dc_mv.append(dict_info)<br>dc_mv<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/6b1ae64eb14f456a8b987605e795b405.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 插入前 先清空</span><br>ct_mv_info.delete_many(&#123;&#125;)<br><span class="hljs-comment"># 插入文档</span><br>ct_mv_info.insert_many(dc_mv)<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/19e9e7a11cef4c4c96a83440b25e6492.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-3-查询MongoDB的数据"><a href="#4-3-查询MongoDB的数据" class="headerlink" title="4.3 查询MongoDB的数据"></a>4.3 查询MongoDB的数据</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python">ct_mv_info.find_one()<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/6d8a6ee9353f4f4ebae990dbf3069aa0.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-4-同样的操作插入影评"><a href="#4-4-同样的操作插入影评" class="headerlink" title="4.4 同样的操作插入影评"></a>4.4 同样的操作插入影评</h2><p>先处理信息，将原先的DataFrame的影评信息转化为可插入到MongoDB的dict字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># list_mv[1] 输出结果 dict_keys([&#x27;reviews&#x27;, &#x27;star&#x27;])</span><br><span class="hljs-comment"># 查询保存的列表数据</span><br><span class="hljs-comment"># for x in list_mv[1].values.to_list():</span><br><span class="hljs-comment">#     print(x)</span><br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-string">    根据之前的存储信息获取所有电影的影评, 封装成可插入MongoDB的 dict</span><br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-keyword">def</span> <span class="hljs-title function_">getAllReviews</span>() -&gt; <span class="hljs-built_in">list</span>[<span class="hljs-built_in">list</span>]:<br>    index = <span class="hljs-number">0</span><br>    reviews = []<br>    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(list_mv)):<br>        <span class="hljs-comment"># 获取每一列</span><br>        rv_cols = list_mv[<span class="hljs-number">0</span>][<span class="hljs-string">&#x27;reviews&#x27;</span>].columns<br>        <span class="hljs-comment"># 表示当前的评论标号</span><br>        i = <span class="hljs-number">0</span><br>        <span class="hljs-comment"># 记录当前电影的所有影评信息</span><br>        dc_reviews = []<br>        <span class="hljs-keyword">for</span> k, rows <span class="hljs-keyword">in</span> list_mv[index][<span class="hljs-string">&#x27;reviews&#x27;</span>].iterrows():<br>            <span class="hljs-comment"># 根据电影ID和当前的评论序号定义_id</span><br>            dict_info = &#123;<span class="hljs-string">&#x27;_id&#x27;</span> : mv_data[<span class="hljs-string">&#x27;mv_id&#x27;</span>][index] + <span class="hljs-built_in">str</span>(i)&#125;<br>            i += <span class="hljs-number">1</span><br>            <span class="hljs-keyword">for</span> col <span class="hljs-keyword">in</span> rv_cols:<br>                dict_info[col] = rows[col]<br>            dc_reviews.append(dict_info)<br>        index += <span class="hljs-number">1</span><br>        reviews.append(dc_reviews)<br>    <span class="hljs-keyword">return</span> reviews<br><br><span class="hljs-comment"># 获取Top10电影的爬取到的所有影评</span><br>dc_reviews = getAllReviews()<br>count = <span class="hljs-number">0</span><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(dc_reviews)):<br>    count += <span class="hljs-built_in">len</span>(dc_reviews[i])<br><span class="hljs-built_in">print</span>(<span class="hljs-string">f&#x27;[INFO] &gt;&gt; 共获取到 <span class="hljs-subst">&#123;<span class="hljs-built_in">len</span>(dc_reviews)&#125;</span> 个电影 <span class="hljs-subst">&#123;count&#125;</span> 个的影评&#x27;</span>)<br><br><span class="hljs-built_in">print</span>(<span class="hljs-string">f&#x27;[INFO] &gt;&gt; 查看其中的一个影评： <span class="hljs-subst">&#123;dc_reviews[<span class="hljs-number">0</span>][:<span class="hljs-number">1</span>]&#125;</span>&#x27;</span>)<br><br></code></pre></td></tr></table></figure>
<p>处理结果：<br><img src="https://img-blog.csdnimg.cn/e01b0213c5a9469ebd82dec1930037d9.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h2 id="4-5-插入影评信息到MongoDB"><a href="#4-5-插入影评信息到MongoDB" class="headerlink" title="4.5 插入影评信息到MongoDB"></a>4.5 插入影评信息到MongoDB</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python">ct_mv_review.delete_many(&#123;&#125;)<br><span class="hljs-keyword">for</span> rv <span class="hljs-keyword">in</span> dc_reviews:<br>    ct_mv_review.insert_many(rv)<br><span class="hljs-comment"># 查看插入结果</span><br>ct_mv_review.find_one()<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/870336e2fc9f48a5b5aebf76d2b42012.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<h1 id="五、基于-PyMongo的数据分析"><a href="#五、基于-PyMongo的数据分析" class="headerlink" title="五、基于 PyMongo的数据分析"></a>五、基于 PyMongo的数据分析</h1><hr>
<h2 id="5-1-计算豆瓣-Top-10-影视的平均评分"><a href="#5-1-计算豆瓣-Top-10-影视的平均评分" class="headerlink" title="5.1 计算豆瓣 Top 10 影视的平均评分"></a>5.1 计算豆瓣 Top 10 影视的平均评分</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs python">ct_mv_info.find_one()<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/31b22972521840a5854fa0e264728dfe.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs py"><span class="hljs-keyword">from</span> bson.code <span class="hljs-keyword">import</span> Code<br><br>mapper = Code(<span class="hljs-string">&quot;&quot;&quot;function()&#123;</span><br><span class="hljs-string">       emit(&#x27;&#x27;, &#123;count:1, mv_star: eval(this.mv_star)&#125;);</span><br><span class="hljs-string">    &#125;</span><br><span class="hljs-string">&quot;&quot;&quot;</span>)<br>reducer = Code(<span class="hljs-string">&quot;&quot;&quot;function(k, v) &#123;</span><br><span class="hljs-string">        reducedVal = &#123;count: 0, mv_star: 0&#125;;</span><br><span class="hljs-string">        for (var idx = 0; idx &lt; v.length; idx++) &#123;</span><br><span class="hljs-string">            reducedVal.count += v[idx].count;</span><br><span class="hljs-string">            reducedVal.mv_star += v[idx].mv_star;</span><br><span class="hljs-string">        &#125;</span><br><span class="hljs-string">        return reducedVal;</span><br><span class="hljs-string">    &#125;;</span><br><span class="hljs-string">&quot;&quot;&quot;</span>)<br><br>finalizer = Code(<span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">        reducedVal.mv_star_avg = reducedVal.mv_star/reducedVal.count;</span><br><span class="hljs-string">        return reducedVal;</span><br><span class="hljs-string">&quot;&quot;&quot;</span>)<br>res = ct_mv_info.map_reduce(<span class="hljs-built_in">map</span> = mapper,reduce=reducer,out=<span class="hljs-string">&#x27;mv_star_avg&#x27;</span>, finalize = finalizer)<br>res<br><br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/f5257a0bde1b415c9ca322d0bdebe679.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs py">ct_mv_star_avg = db.mv_star_avg<br><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> ct_mv_star_avg.find():<br>    <span class="hljs-built_in">print</span>(x)<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/51d95a6d4ad44ec9bb10341e9b898c62.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>根据结果得出，10部电影里的平均评分为9.5分，还是处于相当高的水平，当然这个案例并没有实际意义，主要是为了熟悉MongoDB的MapReduce的基本使用</p>
<h2 id="5-2-统计Top10电影影评的-赞同-x2F-不赞同-的平均比率"><a href="#5-2-统计Top10电影影评的-赞同-x2F-不赞同-的平均比率" class="headerlink" title="5.2 统计Top10电影影评的[赞同 &#x2F; 不赞同]的平均比率"></a>5.2 统计Top10电影影评的[赞同 &#x2F; 不赞同]的平均比率</h2><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs py">ct_mv_review.find_one()<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/a3716363e01b488f9b98d8710d20e6b6.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs py"><span class="hljs-keyword">from</span> bson.code <span class="hljs-keyword">import</span> Code<br><br>mapper = Code(<span class="hljs-string">&quot;&quot;&quot;function()&#123;</span><br><span class="hljs-string">       var a = eval(this.rv_action_agree);</span><br><span class="hljs-string">       var b = eval(this.rv_action_disagree);</span><br><span class="hljs-string">       var rate = 0;</span><br><span class="hljs-string">       if(a &gt; 0 &amp;&amp; b &gt; 0)&#123;</span><br><span class="hljs-string">           rate = b / (a + b);</span><br><span class="hljs-string">           emit(this.rv_mv_id, &#123;count: 1, rate: rate&#125;);</span><br><span class="hljs-string">       &#125; </span><br><span class="hljs-string">    &#125;</span><br><span class="hljs-string">&quot;&quot;&quot;</span>)<br>reducer = Code(<span class="hljs-string">&quot;&quot;&quot;function(k, v) &#123;</span><br><span class="hljs-string">        reducedVal = &#123;count: 0, rate: 0&#125;;</span><br><span class="hljs-string">        for (var i = 0; i &lt; v.length; i++) &#123;</span><br><span class="hljs-string">            reducedVal.count += v[i].count;</span><br><span class="hljs-string">            reducedVal.rate += v[i].rate;</span><br><span class="hljs-string">        &#125;</span><br><span class="hljs-string">        return reducedVal;</span><br><span class="hljs-string">    &#125;;</span><br><span class="hljs-string">&quot;&quot;&quot;</span>)<br><br>finalizer = Code(<span class="hljs-string">&quot;&quot;&quot;</span><br><span class="hljs-string">        reducedVal.rate = reducedVal.rate/reducedVal.count;</span><br><span class="hljs-string">        return reducedVal;</span><br><span class="hljs-string">&quot;&quot;&quot;</span>)<br>res = ct_mv_review.map_reduce(<span class="hljs-built_in">map</span> = mapper,reduce=reducer,out=<span class="hljs-string">&#x27;mv_agree_divide_disagree_rate&#x27;</span>, finalize = finalizer)<br>res<br><br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/4072e8685f2342a0b65365d810d1f55d.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs py">ct_mv_agree_divide_disagree_rate = db.mv_agree_divide_disagree_rate<br><br><span class="hljs-comment"># 查询mongodb文档数据并持久化</span><br>temp1 = []<br><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> ct_mv_agree_divide_disagree_rate.find():<br>    <span class="hljs-built_in">print</span>(x)<br>    temp1.append(x)<br><span class="hljs-built_in">len</span>(temp1)<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/317a05e38ff6438d9945ae95e49078de.png#=60%x" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs py"><span class="hljs-comment"># 准备电影ID到电影名的映射字典</span><br>dict_rv_name = &#123;&#125;<br><span class="hljs-keyword">for</span> mv <span class="hljs-keyword">in</span> ct_mv_info.find():<br>    dict_rv_name[mv[<span class="hljs-string">&#x27;mv_id&#x27;</span>]] = mv[<span class="hljs-string">&#x27;mv_name&#x27;</span>] <br><span class="hljs-comment"># 条件查询, 根据电影 ID 获取到对应的电影名</span><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(temp1)):<br>    <span class="hljs-built_in">id</span> = temp1[i][<span class="hljs-string">&#x27;_id&#x27;</span>]<br>    name = <span class="hljs-string">&#x27;&#x27;</span><br>    <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> ct_mv_info.find(&#123;<span class="hljs-string">&#x27;_id&#x27;</span>: <span class="hljs-built_in">id</span>&#125;):<br>        name = x[<span class="hljs-string">&#x27;mv_name&#x27;</span>]<br>    temp1[i][<span class="hljs-string">&#x27;mv_name&#x27;</span>] = name<br>temp1<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/4ba600f459bd485e9c91a17ad489b768.png#=60%x" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>可视化</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs py"><span class="hljs-keyword">import</span> numpy <span class="hljs-keyword">as</span> np<br><span class="hljs-keyword">import</span> matplotlib.pyplot <span class="hljs-keyword">as</span> plt<br><span class="hljs-keyword">import</span> matplotlib<br>matplotlib.rcParams[<span class="hljs-string">&#x27;font.sans-serif&#x27;</span>] = [<span class="hljs-string">&#x27;SimHei&#x27;</span>]<br>matplotlib.rcParams[<span class="hljs-string">&#x27;font.family&#x27;</span>]=<span class="hljs-string">&#x27;sans-serif&#x27;</span><br><br><span class="hljs-comment"># 柱形的宽度</span><br>bar_width = <span class="hljs-number">0.6</span><br>plt.xticks(rotation=<span class="hljs-number">35</span>)<br>x1 = [x[<span class="hljs-string">&#x27;mv_name&#x27;</span>] <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> temp1]<br><br>y1 = [x[<span class="hljs-string">&#x27;value&#x27;</span>][<span class="hljs-string">&#x27;rate&#x27;</span>] <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> temp1]<br><span class="hljs-comment"># 绘制柱形图</span><br>plt.bar(x=x1, <br>        height=y1, <br>        width=bar_width, <br>        color=[<span class="hljs-string">&#x27;skyblue&#x27;</span>, <span class="hljs-string">&#x27;pink&#x27;</span>],<br>        linewidth=<span class="hljs-number">1.5</span>,<br>    )<br><br><span class="hljs-comment"># 26 # 为每个条形图添加数值标签</span><br><span class="hljs-keyword">for</span> x,y <span class="hljs-keyword">in</span> <span class="hljs-built_in">enumerate</span>(y1):<br>    plt.text(x,y+<span class="hljs-number">0.003</span>,<span class="hljs-string">&#x27;%.3f&#x27;</span> % y,ha=<span class="hljs-string">&#x27;center&#x27;</span>)<br>plt.xlabel(<span class="hljs-string">&#x27;电影名称&#x27;</span>,fontsize=<span class="hljs-number">14</span>, color=<span class="hljs-string">&#x27;blue&#x27;</span>)<br>plt.ylabel(<span class="hljs-string">&#x27;评论分歧(反对)平均占比&#x27;</span>,fontsize=<span class="hljs-number">14</span>, color=<span class="hljs-string">&#x27;red&#x27;</span>)<br>plt.title(<span class="hljs-string">&#x27;豆瓣Top10影视评论分歧占比统计图&#x27;</span>,fontsize=<span class="hljs-number">15</span>, color=<span class="hljs-string">&#x27;green&#x27;</span>)<br>plt.show()<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/bcaab3363b314b82b2bda81c61fe5f91.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br><strong>分析过程：</strong></p>
<p>通过PyMongoDB的MapReduce过程，最终得出的豆瓣Top10部分影评分歧占比统计图如上图所示。</p>
<ol>
<li><p>从整体来看，Top10影视的评论分歧都相对较低，处于<code>[6.1%, 11.1%]</code> 范围。</p>
</li>
<li><p>其中占比最多电影的为《<strong>盗梦空间</strong>》，分歧率为 <code>11.1%</code> ，这意味着有100人评论，那么就有将近11人的观点不被赞同，这跟电影的题材、类型、剧情、演员等多个因素都有关。</p>
</li>
<li><p>占比最少的为《<strong>千与千寻</strong>》，仅为 <code>6.1%</code>，这说明观众们的观点大多是一致的，100人里面只有6人左右的观点不一致。</p>
</li>
</ol>
<p>综上，对于Top10的电影，除了评分、观看数等指标，评论分歧率直观体现了影视的影响力，这意味着观众可以选择这个分歧率较小的电影作为参考，达到更好的观看体验，同时对于同行，能更放心地借鉴其中的一些高深的拍摄手法、剧情演绎方法等。</p>
<h2 id="5-3-统计Top10电影从2021年到今日的评论情况"><a href="#5-3-统计Top10电影从2021年到今日的评论情况" class="headerlink" title="5.3 统计Top10电影从2021年到今日的评论情况"></a>5.3 统计Top10电影从2021年到今日的评论情况</h2><figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs py">ct_mv_review.find_one()<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/c65527842e9b4bfebbeaed230db4a84f.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs py"><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> ct_mv_review.aggregate([&#123;<span class="hljs-string">&#x27;$group&#x27;</span>: &#123;<span class="hljs-string">&#x27;_id&#x27;</span>:<span class="hljs-string">&#x27;$rv_mv_id&#x27;</span>, <span class="hljs-string">&#x27;counter&#x27;</span>:&#123;<span class="hljs-string">&#x27;$sum&#x27;</span>:<span class="hljs-number">1</span>&#125;&#125;&#125;]):<br>    <span class="hljs-built_in">print</span>(x)<br></code></pre></td></tr></table></figure>
<p><img src="https://img-blog.csdnimg.cn/bc74787eec8b4100b31ee1e7392a0edd.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br></pre></td><td class="code"><pre><code class="hljs py"><span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> datetime<br><br>list_rv= []<br>year, month, day = <span class="hljs-number">2021</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span><br><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> ct_mv_review.aggregate([<br>    &#123;<br>        <span class="hljs-comment"># 转化类型</span><br>        <span class="hljs-string">&#x27;$project&#x27;</span>:<br>        &#123;<br>            <span class="hljs-string">&#x27;rv_time&#x27;</span>: <span class="hljs-string">&#x27;$rv_time&#x27;</span>,<br>            <span class="hljs-string">&#x27;rv_info&#x27;</span>: <span class="hljs-string">&#x27;$rv_info&#x27;</span>,<br>            <span class="hljs-string">&#x27;rv_name&#x27;</span>: <span class="hljs-string">&#x27;$rv_name&#x27;</span>,<br>            <span class="hljs-string">&#x27;rv_time_stand&#x27;</span>: <br>            &#123;<br>                <span class="hljs-string">&#x27;$convert&#x27;</span>:<br>                &#123;<br>                    <span class="hljs-string">&#x27;input&#x27;</span>:<span class="hljs-string">&#x27;$rv_time&#x27;</span>,<br>                    <span class="hljs-string">&#x27;to&#x27;</span>: <span class="hljs-string">&#x27;date&#x27;</span>,<br>                    <span class="hljs-string">&#x27;onNull&#x27;</span>: <span class="hljs-string">&#x27;missing rv_time&#x27;</span><br>                &#125;<br>            &#125;,<br>        &#125;,<br><br><br>    &#125;,<br>    &#123;<br>        <span class="hljs-string">&#x27;$match&#x27;</span>: <br>        &#123;<br>            <span class="hljs-string">&#x27;rv_time_stand&#x27;</span>:<br>            &#123;<br>                <span class="hljs-string">&#x27;$gte&#x27;</span>: datetime(year,month,day)<br>            &#125;,<br>        &#125;<br>    &#125;, <br><br>]):<br>    list_rv.append(x)<br><br>dict_rv_info = &#123;&#125;<br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-string">    处理 MongoDB 聚合后的结果 汇总评论</span><br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-keyword">for</span> rv <span class="hljs-keyword">in</span> list_rv:<br>    <span class="hljs-comment"># 前 7 位是电影的ID</span><br>    mv_id = rv[<span class="hljs-string">&#x27;_id&#x27;</span>][:<span class="hljs-number">7</span>]<br>    dict_rv_info[dict_rv_name[mv_id]] = &#123;&#125;<br>    dict_rv_info[dict_rv_name[mv_id]][rv[<span class="hljs-string">&#x27;rv_name&#x27;</span>]] = &#123;<br>            <span class="hljs-string">&#x27;rv_time&#x27;</span>: rv[<span class="hljs-string">&#x27;rv_time&#x27;</span>],<br>            <span class="hljs-string">&#x27;rv_info&#x27;</span>: rv[<span class="hljs-string">&#x27;rv_info&#x27;</span>]<br>    &#125;<br><span class="hljs-built_in">print</span>(<span class="hljs-string">f&#x27;[INFO] &gt;&gt; 已统计完 [<span class="hljs-subst">&#123;<span class="hljs-built_in">len</span>(dict_rv_info)&#125;</span>] 个电影在<span class="hljs-subst">&#123;year&#125;</span>年<span class="hljs-subst">&#123;month&#125;</span>月<span class="hljs-subst">&#123;day&#125;</span>后的影评&#x27;</span>)<br><br><br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-string">    词频统计</span><br><span class="hljs-string">&#x27;&#x27;&#x27;</span><br><span class="hljs-keyword">import</span> jieba<br><span class="hljs-keyword">from</span> wordcloud <span class="hljs-keyword">import</span> WordCloud<br><span class="hljs-comment"># 不需要统计的词汇</span><br>nope = [<span class="hljs-string">&#x27;电影&#x27;</span>, <span class="hljs-string">&#x27;没有&#x27;</span>, <span class="hljs-string">&#x27;一个&#x27;</span>, <span class="hljs-string">&#x27;之后&#x27;</span>, <span class="hljs-string">&#x27;这部&#x27;</span>]<br><span class="hljs-keyword">for</span> k, v <span class="hljs-keyword">in</span> dict_rv_info.items():<br>    dict_word_count = &#123;&#125;<br>    <span class="hljs-comment"># 遍历每个用户的评论</span><br>    <span class="hljs-keyword">for</span> review <span class="hljs-keyword">in</span> v.values():<br>        <span class="hljs-comment"># 遍历每个词</span><br>        <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> jieba.cut(review[<span class="hljs-string">&#x27;rv_info&#x27;</span>]):<br>            <span class="hljs-keyword">if</span>(<span class="hljs-built_in">len</span>(x) &gt;= <span class="hljs-number">2</span>) <span class="hljs-keyword">and</span> x <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> nope:<br>                dict_word_count.setdefault(x, <span class="hljs-number">0</span>)<br>                dict_word_count[x] = dict_word_count[x] + <span class="hljs-number">1</span><br>    <span class="hljs-comment">#生成词云 保存到本地</span><br>    t = WordCloud(<br>        width=<span class="hljs-number">600</span>, height=<span class="hljs-number">480</span>,  <span class="hljs-comment"># 图片大小</span><br>        background_color=<span class="hljs-string">&#x27;white&#x27;</span>,  <span class="hljs-comment"># 背景颜色</span><br>        scale=<span class="hljs-number">10</span>,<br>        font_path=<span class="hljs-string">r&#x27;c:\windows\fonts\simfang.ttf&#x27;</span> ).generate_from_frequencies(dict_word_count)<br>    save_path = <span class="hljs-string">&#x27;./count_images/&#x27;</span> + k + <span class="hljs-string">&#x27;.jpg&#x27;</span><br>    t.to_file(save_path)<br>    <span class="hljs-built_in">print</span>(<span class="hljs-string">f&#x27;[INFO] &gt;&gt; 电影[<span class="hljs-subst">&#123;k&#125;</span>] 评论的词频统计词云生成完毕, 保存位置在[<span class="hljs-subst">&#123;save_path&#125;</span>]&#x27;</span>)<br><span class="hljs-comment"># print(dict_word_count)</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/69ad5edec43e418da9fa0436b76f1be9.png" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>评论词云的结果如下图所示：</p>
<p>美丽人生：<br><img src="https://img-blog.csdnimg.cn/ba3ab45e06f54847b836fa6a63802dea.png#=60%x" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>辛德勒的名单<br><img src="https://img-blog.csdnimg.cn/faf7f285b1c946e8b3f511a062e2b1c6.png#=60%x" srcset="/img/loading.gif" lazyload alt="在这里插入图片描述"><br>这里展示了两部电影的评论词云，而且是在21年1月份以后的评论，在MongoDB的强大支持下，检索某个日期里的文档数据十分遍历，通过这样的方式，我们能感受到电影从去年到现在的影响力。</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" class="category-chain-item">数据库</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/MongoDB/">#MongoDB</a>
      
        <a href="/tags/%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98/">#项目实战</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>MongoDB 项目实战 基于PyMongo的电影影评分析 _ 对数据结果进行可视化展示以及分析 _ 评论词云 _ 分时间段分析</div>
      <div>http://example.com/2022/05/06/MongoDB 项目实战 基于PyMongo的电影影评分析 _ 对数据结果进行可视化展示以及分析 _ 评论词云 _ 分时间段分析/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>John Doe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年5月6日</div>
        </div>
      
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/05/19/Neo4j%20%E9%A1%B9%E7%9B%AE%E5%AE%9E%E6%88%98_%E5%9F%BA%E4%BA%8EPy2Neo%E6%9E%84%E5%BB%BA%E7%94%B5%E5%BD%B1%E7%9F%A5%E8%AF%86%E5%9B%BE%E8%B0%B1/" title="Neo4j 项目实战_基于Py2Neo构建电影知识图谱">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Neo4j 项目实战_基于Py2Neo构建电影知识图谱</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/04/09/Docker%20%E7%AC%94%E8%AE%B04%20Compose%20%E5%AE%B9%E5%99%A8%E7%BC%96%E6%8E%92/" title="Docker 笔记4 compose 容器编排">
                        <span class="hidden-mobile">Docker 笔记4 compose 容器编排</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
